package com.chengxusheji.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.chengxusheji.domain.BusinessCompany;
import com.chengxusheji.domain.CardType;
import com.chengxusheji.domain.CardState;
import com.chengxusheji.domain.MemberCard;

@Service @Transactional
public class MemberCardDAO {

	@Resource SessionFactory factory;
    /*每页显示记录数目*/
    private final int PAGE_SIZE = 10;

    /*保存查询后总的页数*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    /*保存查询到的总记录数*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*添加MemberCard信息*/
    public void AddMemberCard(MemberCard memberCard) throws Exception {
    	Session s = factory.getCurrentSession();
    	s.save(memberCard);
    }

    /*查询MemberCard信息*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<MemberCard> QueryMemberCardInfo(String cardNumber,BusinessCompany companyObj,CardType cardTypeObj,CardState cardStateObj,int currentPage) { 
    	Session s = factory.getCurrentSession();
    	String hql = "From MemberCard memberCard where 1=1";
    	if(!cardNumber.equals("")) hql = hql + " and memberCard.cardNumber like '%" + cardNumber + "%'";
    	if(null != companyObj && !companyObj.getSjad().equals("")) hql += " and memberCard.companyObj.sjad='" + companyObj.getSjad() + "'";
    	if(null != cardTypeObj && cardTypeObj.getTypeId()!=0) hql += " and memberCard.cardTypeObj.typeId=" + cardTypeObj.getTypeId();
    	if(null != cardStateObj && cardStateObj.getStateId()!=0) hql += " and memberCard.cardStateObj.stateId=" + cardStateObj.getStateId();
    	 Query q = s.createQuery(hql);
    	/*计算当前显示页码的开始记录*/
    	int startIndex = (currentPage-1) * this.PAGE_SIZE;
    	q.setFirstResult(startIndex);
    	q.setMaxResults(this.PAGE_SIZE);
    	List memberCardList = q.list();
    	return (ArrayList<MemberCard>) memberCardList;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)// 容器不为这个方法开启事务
    public ArrayList<MemberCard> QueryMemberCardInfo(String cardNumber,BusinessCompany companyObj,CardType cardTypeObj,CardState cardStateObj) { 
    	Session s = factory.getCurrentSession();
    	String hql = "From MemberCard memberCard where 1=1";
    	if(!cardNumber.equals("")) hql = hql + " and memberCard.cardNumber like '%" + cardNumber + "%'";
    	if(null != companyObj && !companyObj.getSjad().equals("")) hql += " and memberCard.companyObj.sjad='" + companyObj.getSjad() + "'";
    	if(null != cardTypeObj && cardTypeObj.getTypeId()!=0) hql += " and memberCard.cardTypeObj.typeId=" + cardTypeObj.getTypeId();
    	if(null != cardStateObj && cardStateObj.getStateId()!=0) hql += " and memberCard.cardStateObj.stateId=" + cardStateObj.getStateId();
    	Query q = s.createQuery(hql);
    	List memberCardList = q.list();
    	return (ArrayList<MemberCard>) memberCardList;
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public ArrayList<MemberCard> QueryAllMemberCardInfo() {
        Session s = factory.getCurrentSession();
        String hql = "From MemberCard";
        Query q = s.createQuery(hql);
        List memberCardList = q.list();
        return (ArrayList<MemberCard>) memberCardList;
    }

    /*计算总的页数和记录数*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public void CalculateTotalPageAndRecordNumber(String cardNumber,BusinessCompany companyObj,CardType cardTypeObj,CardState cardStateObj) {
        Session s = factory.getCurrentSession();
        String hql = "From MemberCard memberCard where 1=1";
        if(!cardNumber.equals("")) hql = hql + " and memberCard.cardNumber like '%" + cardNumber + "%'";
        if(null != companyObj && !companyObj.getSjad().equals("")) hql += " and memberCard.companyObj.sjad='" + companyObj.getSjad() + "'";
        if(null != cardTypeObj && cardTypeObj.getTypeId()!=0) hql += " and memberCard.cardTypeObj.typeId=" + cardTypeObj.getTypeId();
        if(null != cardStateObj && cardStateObj.getStateId()!=0) hql += " and memberCard.cardStateObj.stateId=" + cardStateObj.getStateId();
        Query q = s.createQuery(hql);
        List memberCardList = q.list();
        recordNumber = memberCardList.size();
        int mod = recordNumber % this.PAGE_SIZE;
        totalPage = recordNumber / this.PAGE_SIZE;
        if(mod != 0) totalPage++;
    }

    /*根据主键获取对象*/
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public MemberCard GetMemberCardByCardNumber(String cardNumber) {
        Session s = factory.getCurrentSession();
        MemberCard memberCard = (MemberCard)s.get(MemberCard.class, cardNumber);
        return memberCard;
    }

    /*更新MemberCard信息*/
    public void UpdateMemberCard(MemberCard memberCard) throws Exception {
        Session s = factory.getCurrentSession();
        s.update(memberCard);
    }

    /*删除MemberCard信息*/
    public void DeleteMemberCard (String cardNumber) throws Exception {
        Session s = factory.getCurrentSession(); 
        Object memberCard = s.load(MemberCard.class, cardNumber);
        s.delete(memberCard);
    }

}
